package jgl.vajra.management.part.mine.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.umeng.socialize.media.UMImage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.CarAdvertisementPictureBean;
import jgl.vajra.management.bean.CarMemberPictureBean;
import jgl.vajra.management.bean.MemberApplyBean;
import jgl.vajra.management.databinding.ActivityMemberApplyBinding;
import jgl.vajra.management.listener.MyToolbarViewListener;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.manager.UMShareManager;
import jgl.vajra.management.part.mine.contract.MemberApplyContract;
import jgl.vajra.management.part.mine.viewModel.MemberApplyViewModel;
import jgl.vajra.management.widget.bean.ProfileFileBean;
import jgl.vajra.management.widget.giide.GlideImageLoader;
import jgl.vajra.management.widget.interfaces.ImagePickerListener;
import jgl.vajra.management.widget.interfaces.UploadListener;
import jgl.vajra.management.widget.manager.UploadFileMapManager;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.utils.PickFileToolsV2;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(MemberApplyViewModel.class)
public class MemberApplyActivity extends BaseMVVMActivity<MemberApplyViewModel, ActivityMemberApplyBinding> implements MemberApplyContract.View {

    private String logo;
    private PickFileToolsV2 pickFileTools;
    private MemberApplyBean bean;
    private CarAdvertisementPictureBean advertisementPictureBean;
    private int shareLogo = R.drawable.ic_share_logo;

    @Override
    public int getLayoutId() {
        return R.layout.activity_member_apply;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        bean = new MemberApplyBean();

        mBinding.toolbar.setMyToolbarViewListener(new MyToolbarViewListener() {
            @Override
            public void onRightTextClick() {
                super.onRightTextClick();
                IntentManager.toMemberRulesActivity(MemberApplyActivity.this);
            }
        });

        logo = getIntent().getStringExtra("logo");

        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("logo", logo);
        mViewModel.getCarMemberPictureData(map);

        Map<String, Object> mapAdvertisement = new HashMap<>();
        mapAdvertisement.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);

        mViewModel.getCarAdvertisementPictureData(mapAdvertisement);
    }

    @Override
    public void onShareFriends(int type) {//type:1 750x300   type:2500x200
        if (advertisementPictureBean != null) {
            if (type == 1)
                UMShareManager.getInstance().shareWx(MemberApplyActivity.this, advertisementPictureBean.picture1, advertisementPictureBean.title, advertisementPictureBean.title, new UMImage(this, shareLogo));
            else
                UMShareManager.getInstance().shareWx(MemberApplyActivity.this, advertisementPictureBean.picture2, advertisementPictureBean.title, advertisementPictureBean.title, new UMImage(this, shareLogo));
        }

    }

    @Override
    public void onPicture(int type) {
        if (pickFileTools == null)
            pickFileTools = PickFileToolsV2.init(this);
        pickFileTools.pick(true, 0, new ImagePickerListener() {
            @Override
            public void onPickImageList(List<String> imgs, int tag) {
                super.onPickImageList(imgs, tag);
                String avatarUrl = imgs.get(0);

                Map<String, Object> map = new HashMap<>();
                map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
                map.put("logo", logo);

                map.put("tag", 4);
                String fileId = "";
                switch (type) {
                    case 0:
                        fileId = bean.fileId1;
                        break;
                    case 1:
                        fileId = bean.fileId2;
                        break;
                    case 2:
                        fileId = bean.fileId3;
                        break;
                    case 3:
                        fileId = bean.fileId4;
                        break;
                }
                if (!TextUtils.isEmpty(fileId))
                    map.put("fileId", fileId);

                UploadFileMapManager.getInstance().uploadPathMap(map, avatarUrl, "", new UploadListener() {
                    @Override
                    public void onUploadSuccess(List<ProfileFileBean> file) {

                    }

                    @Override
                    public void onUploadStringSuccess(List<String> file) {
                        GlideImageLoader.onDisplayImage(mBinding.iv1, avatarUrl);
                        mBinding.iv1.setVisibility(View.VISIBLE);
                        switch (type) {
                            case 0:
                                bean.picture1 = avatarUrl;
                                break;
                            case 1:
                                bean.picture2 = avatarUrl;
                                break;
                            case 2:
                                bean.picture3 = avatarUrl;
                                break;
                            case 3:
                                bean.picture4 = avatarUrl;
                                break;
                        }
                        mBinding.setData(bean);
                    }

                    @Override
                    public void onUploadFailed() {

                    }
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (pickFileTools != null) {
            pickFileTools.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onDetermine() {
        if ((TextUtils.isEmpty(bean.picture1) || TextUtils.isEmpty(bean.picture2)) || (TextUtils.isEmpty(bean.picture3) || TextUtils.isEmpty(bean.picture4))) {
            ToastUtils.showShort("请上传前后贴或者两侧贴");
            return;
        }
        ToastUtils.showShort("申请成功");
        finish();
    }

    @Override
    public void returnCarMemberPictureData(List<CarMemberPictureBean> data) {
        if (data != null && data.size() != 0) {
            for (int i = 0; i < data.size(); i++) {
                switch (i) {
                    case 0:
                        bean.picture1 = data.get(i).picture;
                        bean.fileId1 = data.get(i).fileId;
                        break;
                    case 1:
                        bean.picture2 = data.get(i).picture;
                        bean.fileId2 = data.get(i).fileId;
                        break;
                    case 2:
                        bean.picture3 = data.get(i).picture;
                        bean.fileId3 = data.get(i).fileId;
                        break;
                    case 3:
                        bean.picture4 = data.get(i).picture;
                        bean.fileId4 = data.get(i).fileId;
                        break;
                }
            }
            mBinding.setData(bean);
        }
    }

    @Override
    public void returnCarAdvertisementPictureData(CarAdvertisementPictureBean data) {

        advertisementPictureBean = data;
    }
}
